package com.meitianhui.convenitenbuy.widget.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.module.order.model.bean.EmptyInfo;
import com.meitianhui.convenitenbuy.module.wallet.http.WalletApiDataSource;
import com.meitianhui.convenitenbuy.util.MD5Utils;
import com.meitianhui.convenitenbuy.widget.PasswordInputView;

/**
 * Created by Administrator on 2017/11/9.
 */

public class PasswordDialog extends Dialog implements View.OnClickListener, PasswordInputView.onPasswordListener {
    private Context context;      // 上下文
    private int layoutResID;      // 布局文件id
    private ImageView mIvClose;
    private PasswordInputView mPIV;
    private TextView mTvContent;
    private TextView mTvDescription;
    private String password;

    public PasswordDialog(Context context, int layoutResID) {
        super(context, R.style.dialog_custom); //dialog的样式
        this.context = context;
        this.layoutResID = layoutResID;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            Window window = getWindow();
            window.setGravity(Gravity.CENTER); // 此处可以设置dialog显示的位置为居中
            window.setWindowAnimations(R.style.bottom_menu_animation); // 添加动画效果
            setContentView(layoutResID);

            WindowManager windowManager = ((Activity) context).getWindowManager();
            Display display = windowManager.getDefaultDisplay();
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.width = display.getWidth() * 4 / 5; // 设置dialog宽度为屏幕的4/5
            lp.height = display.getHeight() / 2; // 设置dialog高度为屏幕1/2
            getWindow().setAttributes(lp);
        } catch (Exception e) {
            e.printStackTrace();
        }
        setCanceledOnTouchOutside(true);// 点击Dialog外部消失

        mIvClose = (ImageView) findViewById(R.id.iv_dialog_password_close);
        mPIV = (PasswordInputView) findViewById(R.id.piv_dialog_payment);
        mTvContent = (TextView) findViewById(R.id.tv_dialog_payment_content);
        mTvDescription = (TextView) findViewById(R.id.tv_dialog_payment_description);

        mIvClose.setOnClickListener(this);
        mPIV.setFinishListener(this);
    }

    private OnDialogPsdCompleteListener listener;

    @Override
    public void onDifference() {

    }

    @Override
    public void onInputFinish(String psd) {
        comparePassword(psd);
    }

    private void comparePassword(String psd) {
        String passwordSign = MD5Utils.MD5Encode(psd, "");
        WalletApiDataSource.getInstance().comparePassword(InfoProvider.getUserToken(App.getInstance()).getUser_token(), passwordSign).subscribe(new ResponseObserver<BaseResponse<EmptyInfo>>(App.getInstance()) {
            @Override
            public void onSuccess(BaseResponse<EmptyInfo> usableChangeBeanBaseResponse) {
                listener.onPasswordCorrect(PasswordDialog.this);
            }

            @Override
            public void onFail(int code, String msg) {
                super.onFail(code, msg);
                listener.onPasswordError(PasswordDialog.this, msg);
            }
        });
    }

    public interface OnDialogPsdCompleteListener {

        void onPasswordCorrect(PasswordDialog dialog);

        void onPasswordError(PasswordDialog dialog, String msg);

    }

    public void setOnDialogPsdCompareListener(OnDialogPsdCompleteListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.iv_dialog_password_close)
            dismiss();
    }

    public void setPsdDescription(String description) {
        mTvDescription.setText(description);
    }

    public void setPsdContent(String content) {
        mTvContent.setText("¥ " + content);
    }

    public void clearInput() {
        mPIV.clear();
    }

}
